from pip._internal.utils import subprocess

import subprocess

# 数据库连接字符串，根据实际情况修改
database_uri = 'mysql+pymysql://helord:687939g@47.104.144.191:3306/helord'
# 生成的模型文件名称
output_file = 'sql_models.py'

# 构建 sqlacodegen 命令
command = f'sqlacodegen {database_uri}'

try:
    result = subprocess.run(command, shell=True, capture_output=True, text=True, encoding='gbk')
    if result.returncode == 0:
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(result.stdout)
        print(f"模型文件 {output_file} 已成功生成并以 UTF - 8 编码保存。")
    else:
        print(f"执行命令时出错：{result.stderr}")
except Exception as e:
    print(f"发生错误：{e}")